using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModWatch
{

	/// <summary>
	/// A <see cref="ModWatch.ModAlertExtenderBase"/> control that
	/// notifies the user of a change when the targeted
	/// <see cref="System.Web.UI.WebControls.RadioButtonList"/>'s
	/// selected index is changed.
	/// </summary>
	[TargetControlType(typeof(RadioButtonList))]
	public class OnRadioButtonListChangedModAlertExtender : ModAlertExtenderBase
	{

		/// <summary>
		/// Raises the <see cref="System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">
		/// An <see cref="System.EventArgs"/>
		/// object that contains event data.
		/// </param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			this.GettingScriptReferences += new EventHandler<ScriptReferencesEventArgs>(this_GettingScriptReferences);
		}

		/// <summary>
		/// Returns the <see cref="System.Web.UI.ScriptDescriptor"/>s required by
		/// this instance of <see cref="ModWatch.OnRadioButtonListChangedModAlertExtender"/>.
		/// </summary>
		/// <param name="targetControl">
		/// The <see cref="System.Web.UI.WebControls.RadioButtonList"/> being targeted.
		/// </param>
		/// <returns>
		/// A collection of <see cref="System.Web.UI.ScriptDescriptor"/>s
		/// defining this instance of <see cref="ModWatch.OnRadioButtonListChangedModAlertExtender"/>.
		/// </returns>
		protected override IEnumerable<ScriptDescriptor> GetScriptDescriptors(Control targetControl)
		{
			RadioButtonList rbl = (RadioButtonList)targetControl;

			for (int i = 0; i < rbl.Items.Count; i++)
			{
				ScriptBehaviorDescriptor sd = new ScriptBehaviorDescriptor("ModWatch.OnRadioButtonChangedModAlertBehavior", String.Format("{0}_{1}", rbl.ClientID, i));
				AlertBehaviorBase alert = base.GetAlertBehaviorBase();
				if (alert != null)
					sd.AddComponentProperty("alertControl", alert.GetClientComponentID());
				sd.AddProperty("notifyModMessage", this.NotifyModMessage);
				sd.AddProperty("modGroup", this.ModGroup);
				sd.AddProperty("groupName", rbl.UniqueID);

				yield return sd;
			}
		}

		/// <summary>
		/// Registers the scripts needed by the
		/// <see cref="ModWatch.OnRadioButtonListChangedModAlertExtender"/> class.
		/// </summary>
		/// <param name="sender">
		/// The <see cref="System.Object"/> that fired the
		/// <see cref="ModWatch.ModWatchExtenderBase.GettingScriptReferences"/> event.
		/// </param>
		/// <param name="e">
		/// A <see cref="ModWatch.ScriptReferencesEventArgs"/> object that contains the event data.
		/// </param>
		private void this_GettingScriptReferences(object sender, ScriptReferencesEventArgs e)
		{
			e.AddWebResourceScriptReference("ModWatch.Scripts.OnRadioButtonChangedModAlertBehavior.js");
			e.AddWebResourceScriptReference("ModWatch.Scripts.ModAlertRadioButtonTracker.js");
		}

	}

}
